﻿namespace EyeOpen.SecuritySample.Web.UI.Controls
{
    using System;
    using System.Web.Security;
    using System.Web.UI.WebControls;
    using EyeOpen.Dependency;
    using EyeOpen.SecurityService;
    using EyeOpen.Util;

    public partial class CustomLogin : System.Web.UI.UserControl
    {
        protected void LoginLinkButton_Click(object sender, EventArgs e)
        {
            TextBox userNameTextBox = (TextBox) this.loginView.FindControl("userNameTextBox");
            TextBox passwordTextBox = (TextBox) this.loginView.FindControl("passwordTextBox");

            string userName = userNameTextBox.Text;
            string passwordHash = SecurityUtility.ComputeBase64MD5Hash(passwordTextBox.Text);

            if (DependencyContainer
                .Resolve<IMembershipSecurityService>()
                .IsValidUser(userName, passwordHash))
            {
                FormsAuthentication.SetAuthCookie(userName, false);
                this.Response.Redirect(FormsAuthentication.GetRedirectUrl(userName, false));
            }
        }
    }
}